package com.lb.jwitter.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblDKMHDotDKHocVienChiTiet;
import com.lb.jwitter.service.TblMenu;
import com.lb.jwitter.service.TblMenuService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblQuyenMenu;
import com.lb.jwitter.service.TblQuyenMenuService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

@ContextConfiguration(locations="classpath:beans.xml") 
public class HomeAction {

	private List<TblQuyenMenu> tblQuyenMenus;
	private TblQuyenMenuService tblQuyenMenuService;
	private List<TblMenu> tblMenuChas;
	
	  private TblTaiKhoan taiKhoanDangNhap; 
	  
	  public TblTaiKhoan getTaiKhoanDangNhap() { 
	      return taiKhoanDangNhap; 
	  } 
	  public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
	      this.taiKhoanDangNhap = taiKhoanDangNhap; 
	  } 
	  private TblQuyenChiTietService tblQuyenChiTietService;
	  
	@Autowired // Injected by Spring 
	ApplicationContextProvider contextProvider; 
	 	 
	public String execute() throws Exception
	{

		Map session = ActionContext.getContext().getSession();
	     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
	     if( taiKhoanDangNhap == null) return "noprivilege";
	    ApplicationContext context = contextProvider.getApplicationContext(); 
	    
	    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
	    /*if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_CanBo", "sua")){ 
	    	
	    	System.out.println("khong co quyen");
	        return "noprivilege"; 
	    }*/           
		tblQuyenMenuService = (TblQuyenMenuService) context.getBean("tbl_Quyen_MenuClient");

		tblQuyenMenus = tblQuyenMenuService.findtblQuyenMenuByNhomQuyen(taiKhoanDangNhap.getMaNhomQuyen().getId(), 0, 0);
		
	    tblMenuChas = new ArrayList<TblMenu>();
	    
	    for(TblQuyenMenu tbl : tblQuyenMenus){
	    	if(tbl.getMenuId().getMenuCha() == null)
	    		tblMenuChas.add(tbl.getMenuId());	
	    }

		return Action.SUCCESS;

	}
	public List<TblQuyenMenu> getTblQuyenMenus() {
		return tblQuyenMenus;
	}
	public void setTblQuyenMenus(List<TblQuyenMenu> tblQuyenMenus) {
		this.tblQuyenMenus = tblQuyenMenus;
	}
	public List<TblMenu> getTblMenuChas() {
		return tblMenuChas;
	}
	public void setTblMenuChas(List<TblMenu> tblMenuChas) {
		this.tblMenuChas = tblMenuChas;
	}

	
	
	
	
	
}
